package com.tianzj.boot.mvc.service.system.impl;

import java.util.ArrayList;
import java.util.List;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianzj.boot.mvc.entity.system.SysRoleMenu;
import com.tianzj.boot.mvc.repository.system.SysRoleMenuRepository;
import com.tianzj.boot.mvc.service.system.SysRoleMenuService;

@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
	
	@Autowired
	private SysRoleMenuRepository sysRoleMenuRepository;
	
	@Override
	public void saveRoleMenu(Long roleId, String menuIds) {
		sysRoleMenuRepository.deleteByRoleId(roleId);
		if(StringUtils.isNotBlank(menuIds)){
			String[] menuId = menuIds.split(",");
			List<SysRoleMenu> sysRoleMenus=new ArrayList<SysRoleMenu>();
			for (int i = 0; i < menuId.length; i++) {
				SysRoleMenu sysRoleMenu = new SysRoleMenu();
				sysRoleMenu.setMenuId(Long.valueOf(menuId[i]));
				sysRoleMenu.setRoleId(roleId);
				sysRoleMenus.add(sysRoleMenu);
			}
			sysRoleMenuRepository.save(sysRoleMenus);
		}
	}

	@Override
	public List<SysRoleMenu> getRoleMenyByRoleId(Long roleId) {
		return sysRoleMenuRepository.findByRoleId(roleId);
	}

}
